package com.shiwaixiangcun.customer.module.property

import android.graphics.Color
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.google.gson.reflect.TypeToken
import com.jaeger.recyclerviewdivider.RecyclerViewDivider
import com.lzy.okgo.model.HttpParams
import com.shiwaixiangcun.customer.GlobalApi
import com.shiwaixiangcun.customer.R
import com.shiwaixiangcun.customer.adapter.AdapterPaymentRecord
import com.shiwaixiangcun.customer.base.BaseActivity
import com.shiwaixiangcun.customer.entity.CommonData
import com.shiwaixiangcun.customer.entity.PaymentRecordEntity
import com.shiwaixiangcun.customer.entity.PaymentRecordList
import com.shiwaixiangcun.customer.network.NetworkHelper
import com.shiwaixiangcun.customer.utils.JsonUtil
import kotlinx.android.synthetic.main.activity_payment_record.*
import kotlinx.android.synthetic.main.top_bar_white.*
import me.bakumon.statuslayoutmanager.library.OnStatusChildClickListener
import me.bakumon.statuslayoutmanager.library.StatusLayoutManager


/**
 * 物业缴费记录
 */
class PaymentRecordActivity : BaseActivity() {


    private var mNetworkHelper: NetworkHelper? = null
    private var mList: MutableList<PaymentRecordEntity>? = null
    private var mAdapter: AdapterPaymentRecord? = null

    private var statusLayoutManager: StatusLayoutManager? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_payment_record)

        initEmptyView()
        initViewAndEvent()
        initData()
    }

    private fun initEmptyView() {
        val emptyView = LayoutInflater.from(mContext).inflate(R.layout.layout_no_data, null, false)
        emptyView.findViewById<TextView>(R.id.tv_prompt).text = resources.getString(R.string.empty_payment_record)
        statusLayoutManager = StatusLayoutManager.Builder(rv_payment_record)
                .setEmptyLayout(emptyView)
                .setDefaultLayoutsBackgroundColor(Color.WHITE)
                .setErrorLayout(R.layout.layout_error)
                .setOnStatusChildClickListener(object : OnStatusChildClickListener {
                    override fun onErrorChildClick(p0: View?) {
                        initData()
                    }

                    override fun onEmptyChildClick(p0: View?) {
                    }

                    override fun onCustomerChildClick(p0: View?) {
                    }
                })
                .build()
    }

    private fun initData() {
        val httpParams = HttpParams()
        mNetworkHelper?.getRequestWithToken(GlobalApi.paymentRecord, true, httpParams, object : NetworkHelper.NetWorkHelperBack {
            override fun requestSuccess(json: String) {
                val type = object : TypeToken<CommonData<PaymentRecordList<PaymentRecordEntity>>>() {}.type
                val fromJson = JsonUtil.fromJson<CommonData<PaymentRecordList<PaymentRecordEntity>>>(json, type)
                        ?: return

                when (fromJson.responseCode) {
                    1001 -> {

                        if (fromJson.data.list.isEmpty()) {
                            statusLayoutManager?.showEmptyLayout()
                        } else {
                            statusLayoutManager?.showSuccessLayout()
                            mList?.addAll(fromJson.data.list)
                            mAdapter?.notifyDataSetChanged()
                        }
                    }

                }
            }

            override fun requestFail() {
            }
        })
    }

    /**
     * 初始化
     */
    private fun initViewAndEvent() {
        tv_page_name.text = "缴费记录"
        mNetworkHelper = NetworkHelper(mContext, supportFragmentManager)
        back_left.setOnClickListener { finish() }
        mList = ArrayList()
        mAdapter = AdapterPaymentRecord(mList)
        rv_payment_record.layoutManager = LinearLayoutManager(mContext)
        rv_payment_record.adapter = mAdapter
        val divider = RecyclerViewDivider.Builder(mContext)
                .setOrientation(RecyclerViewDivider.VERTICAL)
                .setStyle(RecyclerViewDivider.Style.END)
                .setMarginLeft(16f)
                .setMarginRight(16f)
                .setSize(0.5f)
                .setColorRes(R.color.color_divider_0_1)
                .build()
        rv_payment_record.addItemDecoration(divider)

        mAdapter?.setOnItemClickListener { adapter, view, position ->


            val itemEntity = adapter.data[position] as PaymentRecordEntity
            val bundle = Bundle()
            bundle.putLong("houseId", itemEntity.id)
            readyGo(RecordDetailActivity::class.java, bundle)

        }


    }


}
